﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using BasicDataManagement;
using DataModel;

namespace StaffManagement
{
    /// <summary>
    /// Window1.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        IOpertation op;
        SystemUser user;
        private MainWindow()
        {
            InitializeComponent();
            op = OperationFactory.Create();
            gridAuthority.Visibility = Visibility.Hidden;
            listBoxStaff.Items.Clear();
        }
        public MainWindow(SystemUser user)
            : this()
        {
            this.user = user;
        }

        private void ButtonAddGroup_Click(object sender, RoutedEventArgs e)
        {
            AddGroupWindow win = new AddGroupWindow();
            win.Owner = this;
            win.WindowStartupLocation = WindowStartupLocation.CenterOwner;
            if (win.ShowDialog() == true)
            {
                AuthorityGroup tmpGroup = new AuthorityGroup(win.groupName);
                op.AddAuthorityGroup(tmpGroup);
                TreeViewItem tmptvi = new TreeViewItem();
                tmptvi.Header = tmpGroup.GroupName;
                tmptvi.DataContext = tmpGroup;
                tmptvi.Selected += new RoutedEventHandler(tviGroup_Selected);
                tviAll.Items.Add(tmptvi);
            }
        }


        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            var groups = op.LoadAuthorityGroups();
            tviAll.Items.Clear();
            foreach (var t in groups)
            {
                TreeViewItem tmptvi = new TreeViewItem();
                tmptvi.Header = t.GroupName;
                tmptvi.DataContext = t;
                tmptvi.Selected += new RoutedEventHandler(tviGroup_Selected);
                tviAll.Items.Add(tmptvi);
            }
        }

        void tviGroup_Selected(object sender, RoutedEventArgs e)
        {
            gridAuthority.Visibility = Visibility.Visible;
            gridAuthority.IsEnabled = true;
            AuthorityGroup groupData = (sender as TreeViewItem).DataContext as AuthorityGroup;
            checkBoxCashierManagePermission.IsChecked = groupData.CashierPermission;
            checkBoxDataManagePermission.IsChecked = groupData.DataManagePermission;
            checkBoxMemberManagePermission.IsChecked = groupData.MemberManagePermission;
            checkBoxMoneyManagePermission.IsChecked = groupData.MoneyManagePermission;
            checkBoxReliefPermission.IsChecked = groupData.ReliefPermission;
            checkBoxStaffManagePermission.IsChecked = groupData.StaffManagePermission;
            checkBoxStockManagePermission.IsChecked = groupData.StockManagePermission;
            checkBoxCashFlowPermission.IsChecked = groupData.CashFlowPermission;
            checkBoxCheckCtockPermission.IsChecked = groupData.CheckStockPermission;
            checkBoxPurchasePermission.IsChecked = groupData.PurchasePermission;
            checkBoxReimbursementPermission.IsChecked = groupData.ReimbursementPermission;


            //MessageBox.Show((treeViewGroup.SelectedItem as TreeViewItem).Header.ToString());
            listBoxStaff.ItemsSource = null;
            listBoxStaff.Items.Clear();
            listBoxStaff.ItemsSource = new ObservableCollection<StaffInfo>(op.LoadStaffs((treeViewGroup.SelectedItem as TreeViewItem).Header.ToString()));
            gridStaffInfo.DataContext = null;
            staffGroupCB.ItemsSource = null;
        }

        private void tviAll_Selected(object sender, RoutedEventArgs e)
        {
            if (e.Source.Equals(sender))
            {
                gridAuthority.Visibility = Visibility.Hidden;
                listBoxStaff.ItemsSource = null;
                listBoxStaff.Items.Clear();
                listBoxStaff.ItemsSource = new ObservableCollection<StaffInfo>(op.LoadStaffs());
            }
            gridStaffInfo.DataContext = null;
            staffGroupCB.ItemsSource = null;

        }

        private void BtnPermissionOK_Click(object sender, RoutedEventArgs e)
        {
            AuthorityGroup groupData = (treeViewGroup.SelectedItem as TreeViewItem).DataContext as AuthorityGroup;
            groupData.CashierPermission = (bool)checkBoxCashierManagePermission.IsChecked;
            groupData.DataManagePermission = (bool)checkBoxDataManagePermission.IsChecked;
            groupData.MemberManagePermission = (bool)checkBoxMemberManagePermission.IsChecked;
            groupData.MoneyManagePermission = (bool)checkBoxMoneyManagePermission.IsChecked;
            groupData.ReliefPermission = (bool)checkBoxReliefPermission.IsChecked;
            groupData.StaffManagePermission = (bool)checkBoxStaffManagePermission.IsChecked;
            groupData.StockManagePermission = (bool)checkBoxStockManagePermission.IsChecked;
            groupData.CashFlowPermission = (bool)checkBoxCashFlowPermission.IsChecked;
            groupData.CheckStockPermission = (bool)checkBoxCheckCtockPermission.IsChecked;
            groupData.PurchasePermission = (bool)checkBoxPurchasePermission.IsChecked;
            groupData.ReimbursementPermission = (bool)checkBoxReimbursementPermission.IsChecked;
            op.UpdateAuthorityGroup(groupData);
        }

        private void BtnAddStaff_Click(object sender, RoutedEventArgs e)
        {
            AddStaffWindow staffWin = new AddStaffWindow();
            staffWin.Owner = this;
            staffWin.WindowStartupLocation = WindowStartupLocation.CenterOwner;
            if (staffWin.ShowDialog() == true)
            {
                if ((treeViewGroup.SelectedItem as TreeViewItem).Header.ToString().Equals("所有"))
                {
                    listBoxStaff.ItemsSource = null;
                    listBoxStaff.ItemsSource = new ObservableCollection<StaffInfo>(op.LoadStaffs());
                }
                else
                {
                    listBoxStaff.ItemsSource = null;
                    listBoxStaff.ItemsSource = new ObservableCollection<StaffInfo>(op.LoadStaffs((treeViewGroup.SelectedItem as TreeViewItem).Header.ToString()));
                }
            }
        }

        private void BtnStaffInfoOK_Click(object sender, RoutedEventArgs e)
        {
            if (jobNumTB.Text.Equals(""))
            {
                MessageBox.Show("请先选择人员");
                return;
            }
            StaffInfo tmpStaff = new StaffInfo()
            {
                GroupName = staffGroupCB.Text,
                JobNumber = jobNumTB.Text,
                LoginPWD = pwdTB.Text,
                Name = nameTB.Text,
                Money = Convert.ToDecimal(moneyTB.Text)
            };
            op.UpdateStaff(tmpStaff);
            gridStaffInfo.DataContext = null;
            staffGroupCB.ItemsSource = null;
            if ((treeViewGroup.SelectedItem as TreeViewItem).Header.ToString().Equals("所有"))
            {
                listBoxStaff.ItemsSource = null;
                listBoxStaff.ItemsSource = new ObservableCollection<StaffInfo>(op.LoadStaffs());
            }
            else
            {
                listBoxStaff.ItemsSource = null;
                listBoxStaff.ItemsSource = new ObservableCollection<StaffInfo>(op.LoadStaffs((treeViewGroup.SelectedItem as TreeViewItem).Header.ToString()));
            }
        }

        private void staffLB_Selected(object sender, MouseButtonEventArgs e)
        {
            //(sender as StackPanel).Background = Brushes.Yellow;
            gridAuthority.IsEnabled = false;
            gridStaffInfo.DataContext = null;
            gridStaffInfo.DataContext = listBoxStaff.SelectedItem;
            staffGroupCB.ItemsSource = new ObservableCollection<AuthorityGroup>(op.LoadAuthorityGroups());
            staffGroupCB.SelectedItem = (from c in staffGroupCB.ItemsSource as ObservableCollection<AuthorityGroup>
                                         where c.GroupName == (listBoxStaff.SelectedItem as StaffInfo).GroupName
                                         select c).First();
        }


    }
}
